package org.posta.web.server;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Stage;

public class GuiceInitializer implements ServletContextListener
{
    private static Logger log          = LoggerFactory.getLogger(GuiceInitializer.class);
    
    private String        guiceVarName = Injector.class.getName();
    
    public void contextInitialized(ServletContextEvent servletContextEvent)
    {
        final ServletContext servletContext = servletContextEvent.getServletContext();
        
        Injector injector = Guice.createInjector(Stage.DEVELOPMENT);
        servletContext.setAttribute(guiceVarName, injector);
        log.debug("Created injector");
    }
    
    public void contextDestroyed(ServletContextEvent servletContextEvent)
    {
        servletContextEvent.getServletContext().setAttribute(guiceVarName, null);
        log.debug("Disposed injector");
    }
    
}
